import { Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose'
import { Order } from './order.schema'
import { Model } from 'mongoose'
@Injectable()
export class OrderService {
  constructor(@InjectModel(Order.name) private OrderModel: Model<Order>) {}
  getorderlist() {
    return this.OrderModel.find()
  }
  getWorkbenchData() {
    return 'hello, workbenth data'
  }
  async addorder(body) {
    await this.OrderModel.create(body)
    return '添加成功'
  }
  async getselectorderlist(req, res) {
    const { startDate, endDate, origin, brand, OrderID, status } = req.body
    const query: {
      DealTime?: any
      origin?: string
      status?: any
      OrderID?: number
    } = {}
    if (startDate && endDate) {
      query.DealTime = { $gte: new Date(startDate), $lte: new Date(endDate) }
    }
    if (origin) {
      query.origin = origin
    }
    if (status && Array.isArray(status) && status.length > 0) {
      query.status = { $in: status }
    }
    if (OrderID) {
      query.OrderID = Number(OrderID)
    }
    const data = await this.OrderModel.find(query)
    res.send({
      code: 200,
      data: data,
    })
  }
  async getDate(req, res) {
    let data = await this.OrderModel.find()
    res.send({
      code: 200,
      data: data,
    })
  }
  formatDate(date) {
    const year = date.getFullYear()
    const month = String(date.getMonth() + 1).padStart(2, '0')
    const day = String(date.getDate()).padStart(2, '0')
    return `${year}-${month}-${day}`
  }
  getDateRange(days) {
    const today = new Date()
    const dates = []
    for (let i = 0; i < days; i++) {
      const date = new Date(today)
      date.setDate(today.getDate() - i)
      dates.push(this.formatDate(date))
    }
    return dates.reverse() // 如果你希望日期从最早到最新排列，可以调用 reverse()
  }
}
